package com.cmii.sjw.department.control.jsc.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cmii.sjw.department.control.jsc.model.CampusDetail;

import java.math.BigDecimal;

public class CampusDetailParser {

    public static CampusDetail parse(JSONObject data) {
        CampusDetail entity = new CampusDetail();

        entity.setSchoolName(data.getString("schoolName"));
        entity.setOwnerType(data.getString("ownerType"));
        entity.setSchoolRunningType(data.getString("schoolRunningType"));
        entity.setWebSite(data.getString("webSite"));
        entity.setInternet(data.getString("internet"));
        entity.setWaterSupply(data.getString("waterSupply"));
        entity.setToilet(data.getString("toilet"));
        entity.setWash(data.getString("wash"));
        entity.setHistory(data.getString("history"));

        JSONArray latLong = data.getJSONArray("latLong");
        if (latLong != null && latLong.size() >= 2) {
            entity.setLongitude(latLong.getBigDecimal(0));
            entity.setLatitude(latLong.getBigDecimal(1));
        }

        parseTerminal(data, entity);
        parseTeachingConditions(data, entity);
        parseCoverArea(data, entity);
        parseGrade(data, entity);
        parseStudentTypeNum(data, entity);
        parseAlteration(data, entity);

        return entity;
    }

    private static void parseTerminal(JSONObject data, CampusDetail entity) {
        JSONArray terminal = data.getJSONArray("terminal");
        if (terminal != null) {
            for (int i = 0; i < terminal.size(); i++) {
                JSONObject t = terminal.getJSONObject(i);
                String name = t.getString("name");
                if (StrUtil.contains(name, "教师")) {
                    entity.setTeacherTerminal(t.getIntValue("num"));
                    entity.setTeacherTerminalPercent(t.getString("percent"));
                } else if (StrUtil.contains(name, "学生")) {
                    entity.setStudentTerminal(t.getIntValue("num"));
                    entity.setStudentTerminalPercent(t.getString("percent"));
                }
            }
        }
    }

    private static void parseTeachingConditions(JSONObject data, CampusDetail entity) {
        JSONArray teachingConditions = data.getJSONArray("TeachingConditions");
        if (teachingConditions != null) {
            for (int i = 0; i < teachingConditions.size(); i++) {
                JSONObject item = teachingConditions.getJSONObject(i);
                String name = item.getString("name");
                BigDecimal num = item.getBigDecimal("num");

                if (StrUtil.contains(name, "校园足球")) {
                    entity.setCampusFootball(num.intValue());
                } else if (StrUtil.contains(name, "多媒体")) {
                    entity.setMultimediaClassroom(num.intValue());
                } else if (StrUtil.contains(name, "固定资产")) {
                    entity.setAssetTotal(num);
                } else if (StrUtil.contains(name, "仪器设备")) {
                    entity.setAssetDevice(num);
                }
            }
        }
    }

    private static void parseCoverArea(JSONObject data, CampusDetail entity) {
        JSONArray coverArea = data.getJSONArray("coverArea");
        if (coverArea != null) {
            for (int i = 0; i < coverArea.size(); i++) {
                JSONObject item = coverArea.getJSONObject(i);
                String name = item.getString("name");
                BigDecimal num = item.getBigDecimal("num");
                String percent = item.getString("percent");

                if (StrUtil.contains(name, "绿化")) {
                    entity.setLandGreen(num);
                    entity.setLandGreenPercent(percent);
                } else if (StrUtil.contains(name, "运动")) {
                    entity.setLandSports(num);
                    entity.setLandSportsPercent(percent);
                } else if (StrUtil.equals(name, "其他")) {
                    entity.setLandOther(num);
                    entity.setLandOtherPercent(percent);
                }
            }
        }
    }

    private static void parseGrade(JSONObject data, CampusDetail entity) {
        JSONArray gradeArray = data.getJSONArray("grade");
        if (gradeArray != null && !gradeArray.isEmpty()) {
            JSONObject item = gradeArray.getJSONObject(0);
            JSONArray gradeList = item.getJSONArray("gradeList");
            if (gradeList != null) {
                for (int i = 0; i < gradeList.size(); i++) {
                    JSONObject gradeItem = gradeList.getJSONObject(i);
                    String name = gradeItem.getString("name");
                    int num = gradeItem.getIntValue("num");

                    if (StrUtil.contains(name, "一年级")) entity.setGrade1Num(num);
                    else if (StrUtil.contains(name, "二年级")) entity.setGrade2Num(num);
                    else if (StrUtil.contains(name, "三年级")) entity.setGrade3Num(num);
                    else if (StrUtil.contains(name, "四")) entity.setGrade4Num(num);
                }
            }
        }
    }

    private static void parseStudentTypeNum(JSONObject data, CampusDetail entity) {
        JSONArray studentTypeNum = data.getJSONArray("studentTypeNum");
        if (studentTypeNum != null) {
            for (int i = 0; i < studentTypeNum.size(); i++) {
                JSONObject item = studentTypeNum.getJSONObject(i);
                String name = item.getString("name");
                if (StrUtil.contains(name, "中职")) {
                    entity.setZxsVocational(item.getInteger("zxs"));
                    entity.setZsVocational(item.getInteger("zs"));
                }
            }
        }
    }

    private static void parseAlteration(JSONObject data, CampusDetail entity) {
        JSONArray alteration = data.getJSONArray("alteration");
        if (alteration != null && !alteration.isEmpty()) {
            JSONObject item = alteration.getJSONObject(0);
            JSONArray values = item.getJSONArray("value");
            if (values != null) {
                for (int i = 0; i < values.size(); i++) {
                    JSONObject v = values.getJSONObject(i);
                    String name = v.getString("name");
                    int num = v.getIntValue("num");

                    switch (name) {
                        case "招生": entity.setAlterationEnroll(num); break;
                        case "复学": entity.setAlterationResume(num); break;
                        case "转入": entity.setAlterationTransferIn(num); break;
                        case "其他增加": entity.setAlterationIncrease(num); break;
                        case "毕业": entity.setAlterationGraduate(num); break;
                        case "结业": entity.setAlterationFinish(num); break;
                        case "休学": entity.setAlterationSuspend(num); break;
                        case "退学": entity.setAlterationDrop(num); break;
                        case "死亡": entity.setAlterationDie(num); break;
                        case "转出": entity.setAlterationTransferOut(num); break;
                        case "其他减少": entity.setAlterationReduce(num); break;
                    }
                }
            }
        }
    }
}